package org.netty.server.controller;

import java.util.Date;

import org.netty.server.config.NettyServer;
import org.netty.server.handler.ChannelHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;

@RestController
@RequestMapping(value = "/nettyserver", method = RequestMethod.GET)
public class NettyServerController {

    @Resource
    private NettyServer nettyServer;

    @RequestMapping("/localAddress")
    public String localAddress() {
        return "nettyServer localAddress " + nettyServer.getChannel().localAddress();
    }

    @RequestMapping("/isOpen")
    public String isOpen() {
        return "nettyServer isOpen " + nettyServer.getChannel().isOpen();
    }
    
    @RequestMapping("/sendAll")
    public String sendAll() {
    	if (ChannelHandler.channelGroup.isEmpty()) {
    		return "client never connect !";
    	}
        String str = "群发：" + new Date() + "\r\n";
        ChannelHandler.channelGroup.writeAndFlush(str);
        return "nettyServer sendAll " + str;
    }
}